#include "UGameState.h"
#include "UTerrain.h"
#include "UBuilding.h"
#include "../UIniFile/UIniFile.h"
#include "UUnitOrBuildingType.h"
using namespace std;



TGameState::TGameState(std::string Name, TBuildingTypeVector*  buildingtypes, TUnitTypeVector* unittypes)
{
	BuildingTypes = buildingtypes;
	UnitTypes     = unittypes;

	TIniFile IniFile("Data/Info/Maps/"+Name+".ini");
	Terrain = TTerrainRef( new TTerrain(IniFile.getValue("Map", "HeightMap"),
										IniFile.getValue("Map", "ColorMap")));


	OccupationMap.resize((boost::extents[Width()][Height()]));
	for (int x=0; x< Width(); ++x)
	{
		for (int y=0; y<Height(); ++y)
		{
			OccupationMap[x][y] = NULL;
		}
	}
}

bool TGameState::IsFree(int X, int Y)
{
	return ((X>0 && X<Width() && Y>0 && Y<Height()) && ((OccupationMap[X][Y]==NULL)&&(Terrain->IsFlat(X,Y))));
}

void TGameState::BuildBuilding(TBuildingType* type, int PosX, int PosY, float PosZ)
{
	TBuilding* Building = new TBuilding(type, PosX, PosY, PosZ);

	for (int x=PosX; x< PosX+type->Width; ++x)
	{
		for (int y=PosY; y<PosY+type->Height; ++y)
		{
			OccupationMap[x][y]= Building;	
		}
	}
	Buildings.push_back(Building);

}

